<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" />
    <button>计算</button>
    <script>
      /*
        斐波那契数列
          1 1 2 3 5 8 13 21 34 55
      */

      // function fibonacci(num) {
      //   return num <= 2 ? 1 : fibonacci(num - 1) + fibonacci(num - 2);
      // }

      const worker = new Worker("./calc-worker.js");

      document.querySelector("button").onclick = function () {
        const num = +document.querySelector("input").value;
        // const result = fibonacci(num);
        // console.log(result);
        // 主线程通知分线程，开始计算，传入计算的内容
        worker.postMessage(num);

        // 主线程接受分线程的消息
        worker.onmessage = function (e) {
          console.log(e.data);
        };
      };
    </script>
  </body>
</html>
